<?php
	/**
	 *	Wap Portal Server
	 *
	 *	Copyright (C) 2002 - 2004 Emir Sakic
	 *
	 *	Wap Portal Server
	 *	Developer: Emir Sakic - saka@hotmail.com
	 *  Homepage: http://www.sakic.net
	 *	Date: 15/03/2004
	 * 	Version #: 1.2
	**/

if (isset($update_settings)) {
	if (isset($counter) && $counter != "") {
		if ($counter == 'Yes') {
			$counter = 1;
		} else {
			$counter = 0;
		}
		$query = "UPDATE wps_site SET counter = '$counter' WHERE id = 1";
		$result = mysql_query($query) or die("Update failed");
	}

	if (isset($referers) && $referers != "") {
		$query = "UPDATE wps_site SET referers = '$referers' WHERE id = 1";
		$result = mysql_query($query) or die("Update failed");
	}
}

if (isset($reset)) {
	$query = "UPDATE wps_statistics SET nokia = 0, sonyericsson = 0, motorola = 0, siemens = 0, samsung = 0, lg = 0, panasonic = 0, alcatel = 0, philips = 0, nec = 0, unknown = 0 WHERE id = 1";
	$result = mysql_query($query)
	    or die("Reset failed");
	$reset = 0;
}
  // no caching
  @header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");             // Date in the past
  @header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT"); // always modified
  @header("Cache-Control: no-cache, must-revalidate");           // HTTP/1.1
  @header("Pragma: no-cache");                                   // HTTP/1.0
?>
<script language="JavaScript">
<!--
function validate(){
    var message = "Are you sure you want to reset the statistics?";
    return confirm(message);
	}
//-->
</script>

  <table width="600">
    <tr>
      <td colspan="2"><a href='index.php' border=0>Home</a> &gt; Statistics Management</td>
    </tr>
    <tr>
      <td align="center" width="75%"><b>Statistics</b></td>
      <td align="center" width="25%"><b>Resources</b></td>
    </tr>
    <tr>
      <td valign="top">
      <center><a href="index.php?option=stats&sub=hits">Hits</a> ||
      <a href="index.php?option=stats&sub=agents">Agents</a> ||
      <a href="index.php?option=stats&sub=referers">Referrers</a> ||
      <a href="index.php?option=stats&sub=settings">Settings</a></center>
      <br>
<?php

	if (empty($sub)) {
		$sub = "";
	}

	$query = "SELECT * FROM wps_statistics WHERE id = 1";
	$result = mysql_query($query);
	$row = mysql_fetch_row($result);
	$sum = $row[1]+$row[2]+$row[3]+$row[4]+$row[5]+$row[6]+$row[7]+$row[8]+$row[9]+$row[10]+$row[11];

	switch($sub) {
		case "hits":
			?>
			Index Hits: <b><?php echo $sum; ?></b>
			<form name="form1" method="post" action="index.php?option=stats&sub=hits" onSubmit="return validate()">
				<input type="hidden" name="reset" value="1">
				<input type="submit" name="Submit2" value="Reset">
			</form>
			<?php
			break;
		case "agents" :
			?>
		    <b>Agent Statistics:</b><br>
		    <table width="100%" border="0" cellspacing="0" cellpadding="0">
		    <?php
			if ($sum <> 0){
			for ($t = 1; $t <= 11; $t++){
				$percent = 100*$row[$t]/$sum;
				$percentInt[$t-1] = (int)$percent;
				}
			}
			print "<tr><td>Nokia:&nbsp;</td><td width=\"100%\"><img src=\"images/bar1-l.gif\" width=\"3\" height=\"10\">";
				$width = $percentInt[0] * 3;
				print "<img src=\"images/bar1.gif\" width=\"$width\" height=\"10\">";
				print "<img src=\"images/bar1-r.gif\" width=\"3\" height=\"10\"></td><td>$row[1],&nbsp;</td><td>$percentInt[0]&nbsp;%</td></tr>\n";
			print "<tr><td>SonyEricsson:&nbsp;</td><td><img src=\"images/bar2-l.gif\" width=\"3\" height=\"10\">";
				$width = $percentInt[1] * 3;
				print "<img src=\"images/bar2.gif\" width=\"$width\" height=\"10\">";
				print "<img src=\"images/bar2-r.gif\" width=\"3\" height=\"10\"></td><td>$row[2],&nbsp;</td><td>$percentInt[1]&nbsp;%</td></tr>\n";
			print "<tr><td>Motorola:&nbsp;</td><td><img src=\"images/bar3-l.gif\" width=\"3\" height=\"10\">";
				$width = $percentInt[2] * 3;
				print "<img src=\"images/bar3.gif\" width=\"$width\" height=\"10\">";
				print "<img src=\"images/bar3-r.gif\" width=\"3\" height=\"10\"></td><td>$row[3],&nbsp;</td><td>$percentInt[2]&nbsp;%</td></tr>\n";
			print "<tr><td>Siemens:&nbsp;</td><td><img src=\"images/bar4-l.gif\" width=\"3\" height=\"10\">";
				$width = $percentInt[3] * 3;
				print "<img src=\"images/bar4.gif\" width=\"$width\" height=\"10\">";
				print "<img src=\"images/bar4-r.gif\" width=\"3\" height=\"10\"></td><td>$row[4],&nbsp;</td><td>$percentInt[3]&nbsp;%</td></tr>\n";
			print "<tr><td>Samsung:&nbsp;</td><td><img src=\"images/bar5-l.gif\" width=\"3\" height=\"10\">";
				$width = $percentInt[4] * 3;
				print "<img src=\"images/bar5.gif\" width=\"$width\" height=\"10\">";
				print "<img src=\"images/bar5-r.gif\" width=\"3\" height=\"10\"></td><td>$row[5],&nbsp;</td><td>$percentInt[4]&nbsp;%</td></tr>\n";
			print "<tr><td>LG:&nbsp;</td><td width=\"100%\"><img src=\"images/bar6-l.gif\" width=\"3\" height=\"10\">";
				$width = $percentInt[5] * 3;
				print "<img src=\"images/bar6.gif\" width=\"$width\" height=\"10\">";
				print "<img src=\"images/bar6-r.gif\" width=\"3\" height=\"10\"></td><td>$row[6],&nbsp;</td><td>$percentInt[5]&nbsp;%</td></tr>\n";
			print "<tr><td>Panasonic:&nbsp;</td><td width=\"100%\"><img src=\"images/bar1-l.gif\" width=\"3\" height=\"10\">";
				$width = $percentInt[6] * 3;
				print "<img src=\"images/bar1.gif\" width=\"$width\" height=\"10\">";
				print "<img src=\"images/bar1-r.gif\" width=\"3\" height=\"10\"></td><td>$row[7],&nbsp;</td><td>$percentInt[6]&nbsp;%</td></tr>\n";
			print "<tr><td>Alcatel:&nbsp;</td><td><img src=\"images/bar2-l.gif\" width=\"3\" height=\"10\">";
				$width = $percentInt[7] * 3;
				print "<img src=\"images/bar2.gif\" width=\"$width\" height=\"10\">";
				print "<img src=\"images/bar2-r.gif\" width=\"3\" height=\"10\"></td><td>$row[8],&nbsp;</td><td>$percentInt[7]&nbsp;%</td></tr>\n";
			print "<tr><td>Philips:&nbsp;</td><td><img src=\"images/bar3-l.gif\" width=\"3\" height=\"10\">";
				$width = $percentInt[8] * 3;
				print "<img src=\"images/bar3.gif\" width=\"$width\" height=\"10\">";
				print "<img src=\"images/bar3-r.gif\" width=\"3\" height=\"10\"></td><td>$row[9],&nbsp;</td><td>$percentInt[8]&nbsp;%</td></tr>\n";
			print "<tr><td>NEC:&nbsp;</td><td><img src=\"images/bar4-l.gif\" width=\"3\" height=\"10\">";
				$width = $percentInt[9] * 3;
				print "<img src=\"images/bar4.gif\" width=\"$width\" height=\"10\">";
				print "<img src=\"images/bar4-r.gif\" width=\"3\" height=\"10\"></td><td>$row[10],&nbsp;</td><td>$percentInt[9]&nbsp;%</td></tr>\n";
			print "<tr><td>Unknown:&nbsp;</td><td><img src=\"images/bar5-l.gif\" width=\"3\" height=\"10\">";
				$width = $percentInt[10] * 3;
				print "<img src=\"images/bar5.gif\" width=\"$width\" height=\"10\">";
				print "<img src=\"images/bar5-r.gif\" width=\"3\" height=\"10\"></td><td>$row[11],&nbsp;</td><td>$percentInt[10]&nbsp;%</td></tr>\n";
		        ?>
			    </table>
			<br>
		    <?php
			break;
		case "referers":
			// Check counter / referrer settings
			$query = "SELECT * FROM wps_referers ORDER BY date DESC";
			$result = mysql_query($query);
			$num = mysql_num_rows($result);
			echo "<b>Last $num referrers:</b>\n";
			echo "<br>\n";
			echo "<br>\n";
			for($i = 1; $row = mysql_fetch_object($result); $i++) {
				echo "$i. ";
				$common->setup_emulator($row->referer, $row->referer);
				$common->display_emulator();
				$arrDateTime = explode(" ", $row->date);
				$arrDate = explode("-", $arrDateTime[0]);
				$arrTime = explode(":", $arrDateTime[1]);
				$date = date ("Y-m-d H:i:s", mktime ($arrTime[0],$arrTime[1],$arrTime[2],$arrDate[1],$arrDate[2],$arrDate[0]) + $common->get_time_offset());
				echo ", $date<br>\n";
			}
			echo "<br>\n";
			break;
		case "settings":
			$query = "SELECT * FROM wps_site WHERE id = 1";
			$result = mysql_query($query);
			$row = mysql_fetch_object($result);
			?>
			<form name="form" method="post" action="index.php?option=stats&sub=settings">
			<b>Show counter on WAP frontpage:</b>
			<br>
			<br>
			Yes: <input type="radio" name="counter" value="Yes"<?php if ($row->counter==1) echo " checked"; ?>>
			No:<input type="radio" name="counter" value="No"<?php if ($row->counter!=1) echo " checked"; ?>>
			<br>
        	<br>
			<b>Number of latest referrers logged:</b>
			<br>
			<br>
			<input type="text" size="5" name="referers" value="<?php echo $row->referers; ?>">
			<br>
			<br>
            <input type="hidden" name="update_settings" value="1">
            <input type="submit" name="Submit" value="Submit">
        	</form>
        	<?php
			break;
		default:
			echo "<b>Please select the subsection from the menu above.</b>\n";
			echo "<br>\n";
			echo "<br>\n";
			break;
	}
?>
      </td>
      <td valign="top">
<?php
	$common->setup_default_emulator();
	$common->display_emulator();
?>
      </td>
    </tr>
  </table>
